/* eslint-disable */ 
import { useBoolean, useRequest } from "ahooks";
import React from "react";
import { useNavigate } from "react-router-dom";
import { NavBar, Button, Notify } from "react-vant";
import style from "./style.module.scss";
import { deleteUserFiles, getUserFiles } from "../../../api/myApi";

export interface FileState {
  archivesId: number; // 档案id
  diseaseBefore: string; //既往史
  diseaseMain: string; //主要病状
  diseaseNow: string; //现病史
  treatmentEndTime: number | string; // 结束时间
  treatmentHospitalRecent: string; //最近治疗医院
  treatmentProcess: string; // 治疗过程
  treatmentStartTime: number | string; //开始时间
  userId: number; //用户id
  picture?: string;
}




const Index: React.FC = () => {
  const [isInfo, { setFalse, setTrue }] = useBoolean(true);
  const navigate = useNavigate();
  const { data } = useRequest(async () => {
    const resp = await getUserFiles();
    console.log(resp.result);
    if (resp.result.length) {
      setFalse();
    } else {
      setTrue();
    }
    return resp.result[0] as FileState;
  });



  const handleDelete = async () => {
    if (data) {
      const resp = await deleteUserFiles(data?.archivesId);
      if (resp.status === "0000") {
        Notify.show({ type: "success", message: resp.message });
        setTrue();
      }
    }
  };
  return (
    <div>
      <NavBar title="我的档案" onClickLeft={() => navigate(-1)}></NavBar>
      <div>
        {isInfo ? (
          <div>
            <div style={{ height: 400 }}></div>
            <Button
              type="primary"
              round
              block
              onClick={() => navigate("/my/file/add/edit?code=1")}
            >
              添加
            </Button>
          </div>
        ) : (
          data && (
            <div>
              <div className={style.item}>
                <div className={style.title}>主要病状</div>
                <div>{data.diseaseMain}</div>
              </div>
              <div className={style.item}>
                <div className={style.title}>现病史</div>
                <div>{data.diseaseNow}</div>
              </div>
              <div className={style.item}>
                <div className={style.title}>既往病史</div>
                <div>{data.diseaseBefore}</div>
              </div>
              <div className={style.item}>
                <div className={style.title}>治疗经历</div>
                <div>{data.treatmentProcess}</div>
              </div>
              {data.picture && (
                <div className={style.item}>
                  <div className={style.title}>相关图片</div>
                  <div>
                    <img src={data.picture} alt="" />
                  </div>
                </div>
              )}
              <div>
                <Button type="danger" onClick={() => handleDelete()}>
                  删除
                </Button>
                <button onClick={() => navigate("/my/file/add/edit?code=2")}>
                  编辑
                </button>
              </div>
            </div>
          )
        )}
      </div>
    </div>
  );
};

export default Index;
